package com.tidy.seed.admin.system.controller;

import com.tidy.seed.annotation.ApiLog;
import com.tidy.seed.annotation.ApiPermission;
import com.tidy.seed.annotation.NoToken;
import com.tidy.seed.common.StatusCode;
import com.tidy.seed.common.Result;
import com.tidy.seed.admin.system.dto.LoginUserDTO;
import com.tidy.seed.admin.system.dto.RegisterUserDTO;
import com.tidy.seed.admin.system.vo.LoginResponseVO;
import com.tidy.seed.util.UserThreadLocal;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import com.tidy.seed.admin.system.service.UserService;
import com.tidy.seed.admin.system.entity.User;

import org.springframework.web.bind.annotation.RestController;

// ✅ Swagger 引入
import io.swagger.annotations.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HZ-ZY
 * @since 2025-08-01
 */
@RestController
@Api(tags = "User 接口")
@RequestMapping("/admin/system/user")
@ApiPermission
public class UserController {

    @Resource
    private UserService userService;

    @NoToken
    @ApiOperation(value = "用户注册")
    @ApiLog("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterUserDTO dto) {
        Boolean isSuccess = userService.register(dto);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.fail(StatusCode.CODE_500, "用户名已存在！");
        }
    }

    @NoToken
    @ApiOperation(value = "用户登录")
    @ApiLog("用户登录")
    @PostMapping("/login")
    public Result login(@Valid @RequestBody LoginUserDTO dto) {
        // 调用服务层登录方法
        LoginResponseVO loginResult = userService.login(dto);
        // 判断登录结果是否存在
        if (loginResult != null) {
            // 登录成功：用成功结果包装返回
            return Result.success(loginResult, "登录成功");
        } else {
            // 登录失败：返回错误信息
            return Result.fail(StatusCode.CODE_500, "用户名或密码错误！");
        }
    }

    @ApiOperation(value = "新增或更新User")
    @ApiLog("新增或更新用户")
    @PostMapping
    public Result save(@RequestBody @ApiParam("User 对象") User user) {
        userService.saveOrUpdate(user);
        return Result.success();
    }

    @ApiOperation(value = "根据 ID 删除User")
    @ApiLog("删除用户")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable @ApiParam("ID") Integer id) {
        userService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "批量删除User")
    @ApiLog("批量删除用户")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody @ApiParam("ID 列表") List<Integer> ids) {
        userService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "查询全部User")
    @GetMapping
    public Result findAll() {
        return Result.success(userService.list());
    }

    @ApiOperation(value = "根据 ID 查询User")
    @GetMapping("/{id}")
    public Result findOneById(@PathVariable @ApiParam("ID") Integer id) {
        return Result.success(userService.getById(id));
    }

    @ApiOperation(value = "分页查询User")
    @GetMapping("/page")
    public Result findPage(@RequestParam @ApiParam("页码") Integer pageNum,
                           @RequestParam @ApiParam("页大小") Integer pageSize) {
        return Result.success(userService.findPage(pageNum, pageSize));
    }

    @ApiOperation(value = "token获取用户信息")
    @GetMapping("/userInfo")
    public Result getUserInfo() {
        return Result.success(userService.getById(UserThreadLocal.getUserId()));
    }

    @ApiOperation(value = "获取用户菜单")
    @GetMapping("/getUserMenu")
    public Result getUserMenu() {
        return Result.success(userService.getUserMenu());
    }
}
